package com.example.demo.web.rest.controller;

import com.ffcs.oss.annotation.PermissionAuth;
import com.example.demo.client.evt.sysbasequery.MenuConditionEvt;
import com.example.demo.client.evt.sysbasequery.SaveMenuCheckedEvt;
import com.example.demo.client.vm.sysbasequery.MenuStaticDataVm;
import com.example.demo.client.vm.sysbasequery.MenuTreeVm;
import com.ffcs.oss.common.utils.LocalMessageUtil;
import com.example.demo.enums.MenuTypeEnum;
import com.ffcs.oss.param.out.ServiceResp;
import com.example.demo.service.MenuService;
import com.example.demo.web.rest.evt.IdEvt;
import com.example.demo.web.rest.evt.SaveMenuEvt;
import com.example.demo.web.rest.evt.SsoMenuEvt;
import com.example.demo.web.rest.evt.delMenuEvt;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 菜单管理
 * @Author zhengmf
 * @Date 2018/12/29  15:53
 */
@Api("菜单管理")
@RestController
@RequestMapping("/api/menu")
public class MenuController extends BaseController{
    @Autowired
    private MenuService menuService;

    @ApiOperation("删除菜单")
    @PermissionAuth(code = "OSS-SETTING-MENU-DEL")
    @PostMapping(value = "/delMenu")
    public ServiceResp<Boolean> delMenu(@RequestBody @Validated delMenuEvt evt){
        menuService.delMenu(evt);
        baseQueryClient.clearMenuCache();
        return ServiceResp.getInstance(Boolean.class).success(true);
    }

    @ApiOperation("保存菜单")
    @PermissionAuth(code = "OSS-SETTING-MENU-ADD,OSS-SETTING-MENU-EDIT")
    @PostMapping(value = "/saveMenu")
    public ServiceResp saveMenu(@RequestBody @Validated SaveMenuEvt evt){
        SaveMenuCheckedEvt saveMenuCheckedEvt = new SaveMenuCheckedEvt();
        BeanUtils.copyProperties(evt,saveMenuCheckedEvt);
        ServiceResp<Boolean> serviceResp = baseQueryClient.saveMenuChecked(saveMenuCheckedEvt);
        if(!serviceResp.isSuccess()){
            return serviceResp;
        }
        if(evt.getMenuType().equals(MenuTypeEnum.AUTHORITY_CODE.getValue())
                && StringUtils.isBlank(evt.getPermissionCode())){
            return ServiceResp.getInstance().error("无效的权限码");
        }
        Long menuId = menuService.saveMenu(evt);
        baseQueryClient.clearQryCache();
        return ServiceResp.getInstance(Long.class).success(menuId);
    }

    @ApiOperation("菜单查询接口")
    @PostMapping(value = "/getMenuTreeByCondition")
    public ServiceResp<List<MenuTreeVm>> getMenuTreeByCondition(@RequestBody MenuConditionEvt evt){
        return baseQueryClient.qryMenuTree(evt);
    }

    @ApiOperation("菜单管理静态数据")
    @PostMapping(value = "/getMenuStaticData")
    public ServiceResp<MenuStaticDataVm> getMenuStaticData(){
        return baseQueryClient.getMenuStaticData();
    }

    @ApiOperation("根据id查询菜单名")
    @PostMapping("/getMenuNameById")
    public ServiceResp getMenuNameById(@RequestBody @Validated IdEvt evt){
        return baseQueryClient.getMenuNameById(evt);
    }

    @ApiOperation("上级菜单下拉框值")
    @PostMapping(value = "/getParentMenuTree")
    public ServiceResp getParentMenuTree(@RequestBody IdEvt evt){
        return ServiceResp.getInstance().success(menuService.getParentMenuTree(evt));
    }

    @ApiOperation("ssoLink单点其他系统")
    @PostMapping(value = "/ssoLink")
    public ServiceResp ssoLink(@RequestBody @Valid SsoMenuEvt evt, BindingResult bindingResult){
        try{
            if (bindingResult.hasErrors()) {
                return ServiceResp.getInstance().error(LocalMessageUtil.getMessage(bindingResult.getFieldError().getDefaultMessage()));
            }
            return ServiceResp.getInstance().success(menuService.ssoLink(evt.getSsoType()));
        }catch (Exception e){
            return ServiceResp.getInstance().error(e.getMessage());
        }
    }

    /*@ApiOperation("菜单管理的帮助目录树(选中菜单关联的目录)")
    @PostMapping("/getHelpCatalogTree")
    public ServiceResp<List<HelpCatalogVm>> getHelpCatalogTree(@RequestBody NameEnEvt evt){
        return baseQueryClient.getHelpCatalogTree(evt);
    }*/

}
